package androidtest.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	EditText edittext;
	Button bttlogin;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        bttlogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Thread thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						processLogin();
						Intent intent = new Intent(LoginActivity.this,MainActivity.class);
//						intent.p
						startActivity(intent);
					}
				});
				thread.start();
			}
		});
        
    }
    
    public void init(){
    	edittext = (EditText)findViewById(R.id.editTextUserName);
    	bttlogin = (Button)findViewById(R.id.buttonLogin);
    }
    // process button login
    public void processLogin(){
    	String username = edittext.getText().toString();
    	if (username.length() > 0) {
    		 HttpClient httpclient = new DefaultHttpClient();
    	        HttpPost httppost = new HttpPost("http://sacisc.com/api/test/login.php");

    	        try {
    	            // Add your data
    	            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    	            nameValuePairs.add(new BasicNameValuePair("username",username));
    	            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    	            // Execute HTTP Post Request
    	            HttpResponse response = httpclient.execute(httppost);
    	            Log.i("response ","HttpResponse : "+ response);
    	            String result = inputStreamToString(response.getEntity().getContent());
    	            Log.i("Data ","result : "+result);
    	            
					JSONObject jsonObject = new JSONObject(result);
					boolean suc = jsonObject.getBoolean("success");
					String key = jsonObject.getString("key");
					String mainapi = jsonObject.getString("main_api");
					String sender = jsonObject.getString("sender_id");
					Log.i("duoc khong", "suc " + suc +" -- key ="+key +" --mainapi ="+mainapi +" --sender="+sender);
    	        } catch (ClientProtocolException e) {
    	        } catch (IOException e) {
    	        }catch (JSONException e) {
					e.printStackTrace();
				}
		}else{
			Toast.makeText(getBaseContext(), "Username is required", 500).show();
		}
    }
    
    private String inputStreamToString(InputStream is) {
        String s = "";
        String line = "";

        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) { s += line; }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Return full string
        return s;
    }
}